package com.admin.config.security.oa2;

import com.admin.service.security.impl.CustomOAuth2UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;

//@Configuration
public class OAuth2LoginConfig {

//    @EnableWebSecurity
    public static class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
//
//            http.authorizeRequests().anyRequest().authenticated().and().oauth2Login().userInfoEndpoint().userService(this
//            .oauth2UserService());
            http
                    .authorizeRequests()
                    .anyRequest()
                    .authenticated()
                    .and()
                    .oauth2Login()
                    .userInfoEndpoint()
                    .userService(new CustomOAuth2UserService());
        }

//        @Bean
        public OAuth2AuthorizedClientService authorizedClientService(
                ClientRegistrationRepository clientRegistrationRepository) {
            return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
        }

//        @Bean
        public OAuth2AuthorizedClientRepository authorizedClientRepository(
                OAuth2AuthorizedClientService authorizedClientService) {
            return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);
        }

//        @Bean
        public ClientRegistrationRepository clientRegistrationRepository() {
            return new InMemoryClientRegistrationRepository(this.giteeClientRegistration());
        }

        private ClientRegistration giteeClientRegistration() {
            return ClientRegistration.withRegistrationId("gitee")
                    .clientId("b947d3e59a5e0974d9b3d57f83f8edc11b3c4840411a345aa6f0f6ff341b5a0e")
                    .clientSecret("04af429eac9d44d0fb003321d2cb8303e80682231952f701d56f4d275a3da095")
                    .clientAuthenticationMethod(ClientAuthenticationMethod.POST)
                    .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
//                .redirectUriTemplate("{baseUrl}/login/oauth2/code/{registrationId}")
                    .redirectUriTemplate("{baseUrl}/login/oauth2/code/gitee")
                    .scope("user_info", "projects")
                    .authorizationUri("https://gitee.com/oauth/authorize")
                    .tokenUri("https://gitee.com/oauth/token")
                    .userInfoUri("https://gitee.com/api/v5/user")
                    .userNameAttributeName("id")
//                .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
                    .clientName("jack_test_1")
                    .build();
        }
    }

    public static void main(String[] arges) {
        String ss = "{\"id\":87903,\"login\":\"jackYan\",\"name\":\"jackYan\",\"avatar_url\":\"https://gitee" +
                ".com/assets/no_portrait.png\",\"url\":\"https://gitee.com/api/v5/users/jackYan\",\"html_url\":\"https://gitee" +
                ".com/jackYan\",\"followers_url\":\"https://gitee.com/api/v5/users/jackYan/followers\"," +
                "\"following_url\":\"https://gitee.com/api/v5/users/jackYan/following_url{/other_user}\"," +
                "\"gists_url\":\"https://gitee.com/api/v5/users/jackYan/gists{/gist_id}\",\"starred_url\":\"https://gitee" +
                ".com/api/v5/users/jackYan/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://gitee" +
                ".com/api/v5/users/jackYan/subscriptions\",\"organizations_url\":\"https://gitee" +
                ".com/api/v5/users/jackYan/orgs\",\"repos_url\":\"https://gitee.com/api/v5/users/jackYan/repos\"," +
                "\"events_url\":\"https://gitee.com/api/v5/users/jackYan/events{/privacy}\"," +
                "\"received_events_url\":\"https://gitee.com/api/v5/users/jackYan/received_events\",\"type\":\"User\"," +
                "\"site_admin\":false,\"blog\":null,\"weibo\":null,\"bio\":null,\"public_repos\":3,\"public_gists\":0," +
                "\"followers\":0,\"following\":0,\"stared\":1,\"watched\":2,\"created_at\":\"2014-04-20T15:37:32+08:00\"," +
                "\"updated_at\":\"2019-07-11T00:32:53+08:00\"}";

        GiteeOAuth2User giteeOAuth2User = JSONObject.parseObject(ss, GiteeOAuth2User.class);

//        GiteeOAuth2User giteeOAuth2User = JSON.parseObject(ss, GiteeOAuth2User.class);
        System.out.println(giteeOAuth2User);
        //这就是问题
        //GiteeOAuth2User{id=0, login='null', name='null', avatar_url='null', url='null', html_url='null',
        // followers_url='null', following_url='null', gists_url='null', starred_url='null', subscriptions_url='null',
        // organizations_url='null', repos_url='null', events_url='null', received_events_url='null', type='null',
        // site_admin=false, blog='null', weibo='null', bio='null', public_repos=0, public_gists=0, followers=0, following=0,
        // stared=0, watched=0, created_at='null', updated_at='null', email='null', unconfirmed_email='null', phone='null',
        // private_token='null', total_repos=0, owned_repos=0, total_private_repos=0, owned_private_repos=0, private_gists=0,
        // address='null', authorities=[ROLE_USER], attributes=null}
    }
}
